package com.eric.rabbit.config;

import com.eric.rabbit.entity.topic.TopicReceiver;
import com.eric.rabbit.entity.topic.TopicSender;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author:Eric
 * @date:2021/6/28 通配符（topic）模式
 * [#] -> 匹配多个 red.# : red.1|red.1.2|red.1.2.3(√)   1.red(x) 1.red.1(x) : 以red.开头的都可以匹配,反之不可以
 * [*] -> 匹配单个 red.* : red.1|red.2|(√)  red.1.2(x)  : 以red.开头并且只能匹配一个以点结束的字符串
 * 一个生产者 -> 一个exchange -> 两个队列Queue -> 各自对应的消费者
 * exchange -> Queue1( *.red , yellow.#)
 * exchange -> Queue2( red.* , blue.#)
 * 各自消费各自的消息
 */
@Configuration
public class TopicRabbitConfig {

    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange("topic.exchange");
    }

    @Bean
    public Queue topicQueue1() {
        //代表一个匿名的、非持久的、独占的、自动删除的队列
        return new AnonymousQueue();
    }

    @Bean
    public Queue topicQueue2() {
        //代表一个匿名的、非持久的、独占的、自动删除的队列
        return new AnonymousQueue();
    }

    @Bean
    public Binding topicBind1a() {
        //进行exchange与queue绑定
        return BindingBuilder.bind(topicQueue1()).to(topicExchange()).with("*.red");
    }

    @Bean
    public Binding topicBind1b() {
        //进行exchange与queue绑定
        return BindingBuilder.bind(topicQueue1()).to(topicExchange()).with("yellow.#");
    }

    @Bean
    public Binding topicBind2a() {
        //进行exchange与queue绑定
        return BindingBuilder.bind(topicQueue2()).to(topicExchange()).with("red.*");
    }

    @Bean
    public Binding topicBind2b() {
        //进行exchange与queue绑定
        return BindingBuilder.bind(topicQueue2()).to(topicExchange()).with("blue.#");
    }

    @Bean
    public TopicSender topicSender() {
        return new TopicSender();
    }

    @Bean
    public TopicReceiver topicReceiver() {
        return new TopicReceiver();
    }

}
